package com.wicket.okrframework.run;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
//import com.douliwan.DouliwanHeader;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.common.util.RedisUtil;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;


/**
 * @author ：CHS
 * @since 2022/1/13 16:53
 *
 *
 * @version 1.0
 */
@SpringBootApplication(scanBasePackages = {"com.wicket.okrframework", "com.wicket.okrframework.common.mq.config"})
@MapperScan("com.wicket.okrframework.dal.mapper.mbg")
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.wicket.okrframework.plugin.config", "com.wicket.okrframework", "com.wicket.okrframework.processor.timerdemo"})
@EnableFeignClients(basePackages = {"com.wicket.okrcomponent.integration","com.wicket.okrframework.integration"})
public class ApplicationRunner extends SpringBootServletInitializer implements CommandLineRunner {


    @Value("${security.db.address}")
    private String dbAddress;

    @Value("${security.db.account}")
    private String dbAccount;

    @Value("${security.db.password}")
    private String dbPassword;

    @Value("${file.path}")
    private String filePath;

    @Resource
    private RedisUtil redisUtil;

    private static ApplicationContext startedContext;

//    private static DouliwanHeader douliwan = new DouliwanHeader();
    private static final Logger log = LoggerFactory.getLogger(ApplicationRunner.class);

    public static void main(String[] args) {

        ApplicationContext context = SpringApplication.run(ApplicationRunner.class, args);
        startedContext = context;

    }

    @Override
    public void run(String... args) throws Exception {
        log.info("app start");
        CommonFunctionHelper.getClasses("com.wicket.okrframework.dal.po.mbg");

        CommonFunctionHelper.setFilePath(filePath);
        CommonFunctionHelper.setRedisUtil(redisUtil);
        CommonFunctionHelper.getConfigInfo().put("security.db.address",dbAddress);
        CommonFunctionHelper.getConfigInfo().put("security.db.account",dbAccount);
        CommonFunctionHelper.getConfigInfo().put("security.db.password",dbPassword);
        String useFlag = System.getProperty("use.douliwan");
        String netCard = System.getProperty("use.netcard");
        String ipnum = System.getProperty("use.ipnum");
        String cardSplitor = System.getProperty("use.cardsplitor");
        String str = "";
        if(useFlag!=null && useFlag.equals("use")){
//            str = douliwan.loadInfo();
            System.out.println("douliwan:启动");
        }
        CommonFunctionHelper.setSecStr(str);
        System.out.println(str);



        if(netCard!=null){
            String localIp2 = CommonFunctionHelper.getIp(netCard,Integer.valueOf(ipnum));
            System.out.println(localIp2);

            String localMac2 = CommonFunctionHelper.getMac(netCard,cardSplitor);
            System.out.println(localMac2);
            while(true){
                String dtxt = "";
                JSONObject tenantData = CommonFunctionHelper.getLicenseData("TENANT_SPACE");
                if(tenantData!=null){
                    String url = tenantData.getString("dnsAddresses");
                    String ipAdress = tenantData.getString("ipAddresses");
                    String macCode = tenantData.getString("serverMacCode");
                    String shortName = tenantData.getString("spaceShortName");
                    String authorizationSpaceId = tenantData.getString("authorizationSpaceId");

                    if(authorizationSpaceId!=null && !authorizationSpaceId.isEmpty()){

                        Map<String,String> filter = new HashMap<>();
                        filter.put("unique_space_id",authorizationSpaceId);
                        List<Map<String,String>> uniAppData = CommonFunctionHelper.getDbData("oms_framework_union_app",filter);

                        if(url!=null && !url.isEmpty()){

                            if(CollectionUtil.isNotEmpty(uniAppData)){
                                String unappUrl = uniAppData.get(0).get("website");
                                dtxt = dtxt + unappUrl + "\r\n";
                                if(unappUrl!=null && !unappUrl.isEmpty() && unappUrl.equals(url)){
                                    log.info("unappUrl is 666");
                                    dtxt = "uuu is 666" + "\r\n";
                                }else{
                                    log.info("unappUrl is 888");
                                    dtxt = "uuu is 888" + "\r\n";
                                    int exitCode = SpringApplication.exit(startedContext);
                                    System.exit(exitCode);
                                    return;
                                }
                            }else{
                                log.info("unappUrl is 777");
                                dtxt = "uuu is 777" + "\r\n";
                                int exitCode = SpringApplication.exit(startedContext);
                                System.exit(exitCode);
                                return;
                            }
                        }

                        if(ipAdress!=null && !ipAdress.isEmpty()){
                            boolean bFlag = true;
                            String[] ipList = ipAdress.split(",");
                            String localIp = CommonFunctionHelper.getIp(netCard,Integer.valueOf(ipnum));
                            for(String oneIp : ipList){
                                if(localIp!=null && !localIp.isEmpty() && oneIp.equals(localIp)){
                                    log.info("ipAdress is 666");
                                    dtxt = "sss is 666" + "\r\n";
                                    bFlag = false;
                                    break;
                                }
                            }
                            if(bFlag){
                                log.info("ipAdress is 888");
                                dtxt = "sss is 888" + "\r\n";
                                int exitCode = SpringApplication.exit(startedContext);
                                System.exit(exitCode);
                                return;
                            }
                        }

                        if(macCode!=null && !macCode.isEmpty()){
                            boolean bFlag = true;
                            String[] macList = macCode.split(",");
                            String localMac = CommonFunctionHelper.getMac(netCard,cardSplitor);
                            if(localMac!=null){
                                localMac = localMac.toLowerCase(Locale.ROOT);
                            }
                            for(String oneMac : macList){
                                String compareMac = oneMac.toLowerCase(Locale.ROOT);
                                if(localMac!=null && !localMac.isEmpty() && compareMac.equals(localMac)){
                                    log.info("macCode is 666");
                                    dtxt = "ddd is 666" + "\r\n";
                                    bFlag = false;
                                    break;
                                }
                            }
                            if(bFlag){
                                log.info("macCode is 888");
                                dtxt = "ddd is 888" + "\r\n";
                                int exitCode = SpringApplication.exit(startedContext);
                                System.exit(exitCode);
                                return;
                            }
                        }

                        filter.clear();
                        filter.put("space_id",authorizationSpaceId);
                        List<Map<String,String>> spaceData = CommonFunctionHelper.getDbData("oms_framework_space",filter);
                        if(shortName!=null && !shortName.isEmpty()){

                            if(CollectionUtil.isNotEmpty(spaceData)){
                                String spaceShortName = spaceData.get(0).get("abbreviation");
                                if(shortName!=null && !shortName.isEmpty() && shortName.equals(spaceShortName)){
                                    log.info("shortName is 666");
                                    dtxt = "se is 666" + "\r\n";
                                }else{
                                    log.info("shortName is 888");
                                    dtxt = "se is 888" + "\r\n";
                                    int exitCode = SpringApplication.exit(startedContext);
                                    System.exit(exitCode);
                                    return;
                                }
                            }else{
                                log.info("shortName is 777");
                                dtxt = "se is 777" + "\r\n";
                                int exitCode = SpringApplication.exit(startedContext);
                                System.exit(exitCode);
                                return;
                            }
                        }
                    }
                }
                try{
                    Thread.sleep(60000);
                }catch (Exception e){

                }
            }
        }



    }

}
